/**
 * 轻消息提示
 * @param 'msg'
 * @param {config} 配置对象
 * */
export const showToast = (msg, config = {}) => {
  config.title = msg;
  config.icon = config.icon || 'none';
  wx.showToast(config);
}

/**
 * 检验手机号
 * @param val
 * */
export const checkPhone = (val) => {
  if ((/^1[345789]\d{9}$/.test(val))) {
    return true;
  } else {
    return false
  }
}

/**
 * 检验密码 (8~20位)
 * @param val
 * */
export const checkPwd = (val) => {
  if ((/^(?=.*[a-z A-Z])(?=.*\d)[^]{8,20}$/.test(val))) {
    return true;
  } else {
    return false
  }
}

/**
 * 验证码定时器
 * @param obj
 *  @parma time 定时时间
 *  @parma (finish) 完成时
 *  @parma (change) 变化时
 * */
export const CaptchaTimer = (obj) => {
  let num = obj.time || 60;
  let interval = setInterval(callback => {
    if (num === 0) {
      typeof obj.finish === 'function' && obj.finish(num);
      clearInterval(interval);
    } else {
      typeof obj.change === 'function' && obj.change(num);
    }
    num--;
  }, 1000);
}

/**
 * 判断是否函数
 * @param 'name'
 * */
export const isFun = (name) => {
  return typeof name === 'function' ? true : false
}

/**
 * 判断对象是否空
 * */
export const objIsNull = (obj) => {
  return Object.getOwnPropertyNames(obj).length = 0 ? true : false;
}

/**
 * 检测是否授权
 * @param 'type'
 * @param (callback)
 * */
export const isAuthorization = (type, callback) => {
  wx.getSetting({
    success(res) {
      if (res.authSetting['scope.' + type]) {
        callback(true);
      } else {
        callback(false);
      }
    }
  })
}

/**
 * 选中上传图片来源
 * @param [actions]
 * @param (callback)
 * */ 
export const uploadSourceType = (actions = [], callback) => {
  let Actions = actions.length > 0 ?
   actions : ['打开相册', '打开相机'];

  wx.showActionSheet({
    itemList: Actions,
    success(res) {
      let tap_index = res.tapIndex;
      let source_type = ['album', 'camera'];
      wx.chooseImage({
        sourceType: source_type[tap_index],
        success(res) {
          if(typeof callback === 'function') callback(res);
          else throw new Error(`${callback} is not Function`);
        },
      })
    }
  })
}

/**
 * @explain {obj} 对象
 *  @param 'url' 上传链接
 *  @param [filePath] 上传文件(数组类型)
 *  @param 'filePathName' filePath的指定名（指定字段）
 *  @param 'name'
 *  @param (success) 上传成功回调方法
 *  @param (fail) 失败回调方法
 *  @param (complete) 上传完成回调方法
 *  @param @formData 参数
 * */
export const uploadFile = (obj) => {
  let fileNum = 0, successUp = 0, failUp = 0;
  
  let startUploadFile = (obj) => {
    wx.showLoading({
      title: '上传中...',
      mask: true
    });

    if (obj.filePath.length > 0) {
      wx.uploadFile({
        url: obj.url,
        filePath: obj.filePath[fileNum][obj.filePathName] || obj.filePath[fileNum],
        name: obj.name,
        header: obj.header,
        formData: typeof obj.formData === 'function' ? obj.formData(fileNum) : obj.formData,
        success(res) {
          successUp ++;
          typeof obj.success === 'function' && obj.success(res, fileNum);
        },

        fail(res) {
          failUp++;
          showToast('总共' + failUp + '张上传失败！');
          typeof obj.fail === 'function' && obj.fail(res);
        },

        complete(res) {
          fileNum ++;
          if (fileNum < obj.filePath.length) {
            startUploadFile(obj, fileNum, successUp, failUp);
          } else if (fileNum == obj.filePath.length) {
            wx.hideLoading();
            typeof obj.complete === 'function' && obj.complete(res);
          }
        }
      });
    }
  }
  startUploadFile(obj);
}

